package com.cqc.listviewchoicedemo;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ListViewCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cqc.listviewchoicedemo.bean.Person;
import com.cqc.listviewchoicedemo.bean.Student;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cui on 2017/3/18.
 */

public class DuplicateMultipleChoiceActivity extends AppCompatActivity {
    private Context context = this;
    private static final String TAG = "Duplicate";
    private List<Student> list = new ArrayList<>();
    private ListView listView;
    private Button btn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_duplicate_multiple_choice);

        initData();
        listView = (ListView) findViewById(R.id.listView);
        btn = (Button) findViewById(R.id.btn);

        MyAdapter adapter = new MyAdapter();
        listView.setAdapter(adapter);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                long[] checkItemIds = listView.getCheckItemIds();
                StringBuffer sb2 = new StringBuffer();
                for (int i = 0; i < checkItemIds.length; i++) {
                    sb2.append(checkItemIds[i]+",");
                }
                Toast.makeText(context,sb2.toString(),Toast.LENGTH_SHORT).show();
                Log.d(TAG, "sb2=" + sb2.toString());
            }
        });
    }

    private void initData() {
        for (int i = 0; i < 100; i++) {
            Student student = new Student();
            student.setName("张" + i);
            student.setAge(i);
            list.add(student);
        }
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
            MyViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_duplicate_myltiple_chice, viewGroup, false);
                holder = new MyViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (MyViewHolder) convertView.getTag();
            }

            Student student = list.get(position);
            holder.tv_name.setText("姓名：" + student.getName());
            holder.tv_age.setText("年龄：" + student.getAge());
            return convertView;
        }
    }

    public class MyViewHolder {
        TextView tv_name;
        TextView tv_age;
        ImageView iv;

        public MyViewHolder(View itemView) {
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_age = (TextView) itemView.findViewById(R.id.tv_age);
            iv = (ImageView) itemView.findViewById(R.id.iv);
        }
    }
}
